<?php

namespace app\third\controller;

use app\third\model\Mobile as MobileModel;
use think\facade\Db;

class Mobile extends Controller
{
    /**
     * 下单[预充模式].
     */
    public function createOrder()
    {
        if ($this->agent['settle_type'] != SETTLE_TYPE_PREPAY) {
            return fail('暂无接口调用权限', CODE_MISS_PERMISSION);
        }

        $data = input('post.');

        Db::name('mobile_third_request_log')
            ->insert([
                'agent_id' => $this->agent['id'],
                'params' => json_encode($data, JSON_UNESCAPED_UNICODE),
                'ip' => request()->ip(),
            ]);

        $data['agent'] = $this->agent;
        $data['settle_type'] = SETTLE_TYPE_PREPAY;
        $model = new MobileModel();


        return $model->createOrder($data);
    }

    /**
     * 下单[返佣模式].
     */
    public function createRebateOrder()
    {
        if ($this->agent['settle_type'] != SETTLE_TYPE_PROFIT) {
            return fail('暂无接口调用权限', CODE_MISS_PERMISSION);
        }
        // if (isProd()) {
        //     return fail('暂未开通');
        // }
        $data = input('post.');

        Db::name('mobile_third_request_log')
            ->insert([
                'agent_id' => $this->agent['id'],
                'params' => json_encode($data, JSON_UNESCAPED_UNICODE),
                'ip' => request()->ip(),
            ]);

        $data['agent'] = $this->agent;
        $data['settle_type'] = SETTLE_TYPE_PROFIT;
        $model = new MobileModel();

        return $model->createRebateOrder($data);
    }

    /**
     * 订单查询.
     */
    public function queryOrder()
    {
        $data = input('post.');
        $data['agent'] = $this->agent;
        $model = new MobileModel();

        return $model->queryOrder($data);
    }

    /**
     * 查询手机运营商.
     */
    public function queryIspType()
    {
        $mobile = input('post.mobile');
        if (empty($mobile)) {
            return fail('请输入手机号码');
        }
        if (!checkMobile($mobile)) {
            return fail('手机号码格式有误');
        }
        $ispType = getMobileIsp($mobile);

        return success('操作成功', [
            'ispType' => $ispType,
        ]);
    }
}
